{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json, os\n",
    "\n",
    "db = \"postgres\"\n",
    "\n",
    "system_view_path = f\"../../../knowledge_collection/{db}/knob_info/system_view.json\"\n",
    "knobs = []\n",
    "with open(system_view_path, \"r\") as file:\n",
    "    system_view = json.load(file)\n",
    "    for key, value in system_view.items():\n",
    "        knobs.append(key)\n",
    "    knobs.sort()\n",
    "\n",
    "# muli-source knowledge and summary\n",
    "suggestions = dict()\n",
    "path = f\"../../../knowledge_collection/{db}/knowledge_sources/\"\n",
    "for knob in knobs:\n",
    "    suggestions[knob] = dict()\n",
    "    for i in [\"gpt\", \"web\", \"manual\"]:\n",
    "        file_path = os.path.join(path, i, f\"{knob}.txt\")\n",
    "        if os.path.exists(file_path):\n",
    "            with open(file_path, 'r') as file:\n",
    "                txt = file.read()\n",
    "            suggestions[knob][i] = txt\n",
    "        else:\n",
    "            suggestions[knob][i] = None\n",
    "    summary_path = os.path.join(f\"../../../knowledge_collection/{db}/tuning_lake\", f\"{knob}.txt\")\n",
    "    if os.path.exists(summary_path):\n",
    "        with open(summary_path, 'r') as file:\n",
    "            txt = file.read()\n",
    "            suggestions[knob][\"summary\"] = txt\n",
    "    else:\n",
    "        suggestions[knob][\"summary\"] = None\n",
    "    \n",
    "print(suggestions)\n",
    "with open(f\"../suggestion_{db}.json\", 'w') as file:\n",
    "    json.dump(suggestions, file, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# integrate structured knowledge\n",
    "structured_path = f\"../../../knowledge_collection/{db}/structured_knowledge\"\n",
    "\n",
    "structured_knowledge = dict()\n",
    "for knob in knobs:\n",
    "    structured_knowledge[knob] = dict()\n",
    "    file_path = os.path.join(structured_path, \"normal\", f\"{knob}.json\")\n",
    "    if os.path.exists(file_path):\n",
    "        with open(file_path, 'r') as file:\n",
    "            txt = json.load(file)\n",
    "            structured_knowledge[knob] = txt\n",
    "    else:\n",
    "        structured_knowledge[knob][\"min_value\"] = None\n",
    "        structured_knowledge[knob][\"max_value\"] = None\n",
    "        structured_knowledge[knob][\"suggested_values\"] = []\n",
    "\n",
    "    file_path = os.path.join(structured_path, 'special', f\"{knob}.json\")\n",
    "    if os.path.exists(file_path):\n",
    "        with open(file_path, 'r') as file:\n",
    "            txt = json.load(file)\n",
    "            print(txt)\n",
    "            print(knob)\n",
    "            structured_knowledge[knob][\"special_knob\"] = txt[\"special_knob\"]\n",
    "            if structured_knowledge[knob][\"special_knob\"]:\n",
    "                structured_knowledge[knob][\"special_value\"] = txt[\"special_value\"]\n",
    "            else:\n",
    "                structured_knowledge[knob][\"special_value\"] = None\n",
    "    else:\n",
    "        structured_knowledge[knob][\"special_knob\"] = None\n",
    "        structured_knowledge[knob][\"special_value\"] = None\n",
    "\n",
    "print(structured_knowledge)\n",
    "with open(f\"../structured_knowlegde_{db}.json\", 'w') as file:\n",
    "    json.dump(structured_knowledge, file, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# convert suggested_values to string\n",
    "import json,os\n",
    "\n",
    "with open(f\"/home/knob/GPTuner/src/demo/structured_knowlegde_{db}.json\", 'r') as f:\n",
    "    data = json.load(f)\n",
    "    for knob, value in data.items():\n",
    "        if value[\"suggested_values\"] is None:\n",
    "            value[\"suggested_values\"] = []\n",
    "            data[knob] = value\n",
    "\n",
    "with open(f\"/home/knob/GPTuner/src/demo/structured_knowlegde_{db}.json\", 'w') as f:\n",
    "    json.dump(data, f, indent=4)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
